package com.asnike.selector{
	import fl.controls.ScrollBar;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;

	public class ItemSelector extends BasicSelector{
		
		private const ITEM_TYPE_KITCHEN:String = 'kitchen';
		private const ITEM_TYPE_LIVING:String = 'living';
		private const ITEM_TYPE_BED:String = 'bed';
		
		public var itemList:CampItemBarList;
		public var btnStore:Sprite;
		public var btnFinish:Sprite;
		
		private var _itemsType0:CampItemList;
		private var _itemsType1:CampItemList;
		private var _itemsType2:CampItemList;
		private var _itemContainer:Sprite;
		private var _scrollBar:ScrollBar;
		private var _clickBtnNextCallback:Function;
		
		public function ItemSelector(){
			super();
			initItems();
			initScrollBar();
			initEvent();
			initButton();
		}
		private function initItems():void{
			var mask:Shape;
			
			_itemContainer = new Sprite;
			_itemsType0 = new CampItemList;
			_itemsType1 = new CampItemList;
			_itemsType2 = new CampItemList;
			_itemsType0.setCategory( '주방' );
			_itemsType1.setCategory( '거실' );
			_itemsType2.setCategory( '침실' );
			_itemContainer.addChild( _itemsType0 );
			_itemContainer.addChild( _itemsType1 );
			_itemContainer.addChild( _itemsType2 );
			_itemsType0.setItemClickCallback( itemClick );
			_itemsType1.setItemClickCallback( itemClick );
			_itemsType2.setItemClickCallback( itemClick );
			
			mask = new Shape;
			mask.graphics.beginFill( 0 );
			mask.graphics.drawRect( 0, 0, 400, 537 );
			mask.graphics.endFill();
			addChild( mask );
			_itemContainer.mask = mask;
			mask.x = 32;
			mask.y = 220;
			addChild( _itemContainer );
		}
		private function itemClick( $name:String, $price:int ):void{
			itemList.addItem( $name, $price );
		}
		private function initScrollBar():void{
			_scrollBar = new ScrollBar;
			_scrollBar.setScrollProperties( 0, 0, 300 );
			_scrollBar.addEventListener( Event.SCROLL, scroll );
			_scrollBar.setSize( 10, 460 );
			_scrollBar.x = 450;
			_scrollBar.y = 220;
			addChild( _scrollBar );
		}
		private function scroll( $e:Event ):void{
			_itemContainer.y = 220 -_scrollBar.scrollPosition;
		}
		private function initEvent():void{
			addEventListener( Event.ADDED_TO_STAGE, addedToStage );
		}
		private function addedToStage( $e:Event ):void{
			activateMouseScroll();
		}
		private function activateMouseScroll():void{
			stage.addEventListener( MouseEvent.MOUSE_WHEEL, wheel );
		}
		private function wheel( $e:MouseEvent ):void{
			if( $e.delta > 1 ){
				_scrollBar.scrollPosition -= 5;
			}else if( $e.delta < -1 ){
				_scrollBar.scrollPosition += 5;
			}
		}
		
		override public function setData( $data:XML ):void{
			showItems( $data.items[0] );
		}
		private function showItems( $data:XML ):void{
			var i:int, j:int;
			
			for( i = 0, j = $data.item.length() ; i < j ; ++i ){
				switch( $data.item[i].type.toString() ){
				case ITEM_TYPE_KITCHEN:
					_itemsType0.addItem( $data.item[i].name.toString(), $data.item[i].price.toString(), $data.item[i].image.toString() );
					break;
				case ITEM_TYPE_LIVING:
					_itemsType1.addItem( $data.item[i].name.toString(), $data.item[i].price.toString(), $data.item[i].image.toString() );
					break;
				case ITEM_TYPE_BED:
					_itemsType2.addItem( $data.item[i].name.toString(), $data.item[i].price.toString(), $data.item[i].image.toString() );
					break;
				}
			}
			
//			_itemsType0.y = 40;
			_itemsType1.y = _itemsType0.y + _itemsType0.height + 10;
			_itemsType2.y = _itemsType1.y + _itemsType1.height + 10;
			_itemContainer.x = 32;
			_itemContainer.y = 220;
		}
		private function initButton():void{
			btnFinish.buttonMode = true;
			btnFinish.addEventListener( MouseEvent.CLICK, function( $e:MouseEvent ):void{
				if( _clickBtnNextCallback is Function ){
					_clickBtnNextCallback( 4 );
				}
			} );
		}
		public function setClickBtnNextCallback( $function:Function ):void{
			_clickBtnNextCallback = $function;
		}
		
		public function show():void{
			visible = true;
			activateMouseScroll();
		}
		public function hide():void{
			visible = false;
			inactivateMouseScroll();
		}
		private function inactivateMouseScroll():void{
			if( stage ){
				stage.removeEventListener( MouseEvent.MOUSE_WHEEL, wheel );
			}
		}
		
		public function setUpperImage( $id:String ):void{
			var cls:Class, upper:Bitmap;
			
			cls = getDefinitionByName( $id ) as Class;
			upper = new Bitmap( new cls );
			addChild( upper );
		}
		
		public function getResult():Array{
			return itemList.getSelectedItems();
		}
	}
}